package com.csu.pet.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.pet.demo.domain.Account;
import com.csu.pet.demo.domain.Log;
import com.csu.pet.demo.domain.divide.AccountDetail;
import com.csu.pet.demo.domain.divide.BannerData;
import com.csu.pet.demo.domain.divide.Profile;
import com.csu.pet.demo.domain.divide.SignOn;
import com.csu.pet.demo.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AccountService {
    @Autowired
    AccountDetailMapper accountDetailMapper;
    @Autowired
    ProfileMapper profileMapper;
    @Autowired
    SignOnMapper signOnMapper;
    @Autowired
    BannerDataMapper bannerDataMapper;
    @Autowired
    LogMapper logMapper;


    private AccountDetail accountDetail;
    private SignOn signOn;
    private BannerData bannerData;
    private Profile profile;

    //根据username获得account
    public Account getAccount(String username) {
        Account account = null;
        //分别在四个表中查出数据，存入对应的对象
        accountDetail = accountDetailMapper.selectById(username);
        //如果数据库有这个，则合并
        if(accountDetail!=null) {
            signOn = signOnMapper.selectById(username);
            profile = profileMapper.selectById(username);
            bannerData = bannerDataMapper.selectById(profile.getFavouriteCategoryId());
            //对应属性合并到account
            account = new Account();
            account.setUsername(username);
            account.setPassword(signOn.getPassword());
            account.setAddress1(accountDetail.getAddress1());
            account.setAddress2(accountDetail.getAddress2());
            account.setEmail(accountDetail.getEmail());
            account.setFirstName(accountDetail.getFirstName());
            account.setLastName(accountDetail.getLastName());
            account.setCity(accountDetail.getCity());
            account.setCountry(accountDetail.getCountry());
            account.setStatus(accountDetail.getStatus());
            account.setPhone(accountDetail.getPhone());
            account.setZip(accountDetail.getZip());
            account.setState(accountDetail.getState());
            account.setListOption(profile.isListOption());
            account.setBannerOption(profile.isBannerOption());
            account.setLanguagePreference(profile.getLanguagePreference());
            account.setFavouriteCategoryId(profile.getFavouriteCategoryId());
            account.setBannerName(bannerData.getBannerName());
        }
        return account;
    }

    //根据username、password获得account
    public Account getAccount(String username, String password) {
        Account account = null;
        QueryWrapper<SignOn>queryWrapper = new QueryWrapper<>();
        signOn = signOnMapper.selectOne(queryWrapper.nested(i->i.eq("username",username).eq("password",password)));

        if(signOn!=null) {
            accountDetail = accountDetailMapper.selectById(username);
            profile = profileMapper.selectById(username);
            bannerData = bannerDataMapper.selectById(profile.getFavouriteCategoryId());

            account = new Account();
            account.setUsername(username);
            account.setPassword(signOn.getPassword());
            account.setAddress1(accountDetail.getAddress1());
            account.setAddress2(accountDetail.getAddress2());
            account.setEmail(accountDetail.getEmail());
            account.setFirstName(accountDetail.getFirstName());
            account.setLastName(accountDetail.getLastName());
            account.setCity(accountDetail.getCity());
            account.setCountry(accountDetail.getCountry());
            account.setStatus(accountDetail.getStatus());
            account.setPhone(accountDetail.getPhone());
            account.setZip(accountDetail.getZip());
            account.setState(accountDetail.getState());
            account.setListOption(profile.isListOption());
            account.setBannerOption(profile.isBannerOption());
            account.setLanguagePreference(profile.getLanguagePreference());
            account.setFavouriteCategoryId(profile.getFavouriteCategoryId());
            account.setBannerName(bannerData.getBannerName());
        }

        return account;
    }



    //插入新account
    public boolean insertAccount(Account account) {
        //判断数据库中是否已经包含这个username
        AccountDetail isExist = accountDetailMapper.selectById(account.getUsername());
        System.out.println("准备插入");
        if(isExist == null){
            //将account中对应属性放在相应的对象中，分别插入到相应的表中

            //插入到account表
            accountDetail.setUserid(account.getUsername());
            accountDetail.setAddress1(account.getAddress1());
            accountDetail.setAddress2(account.getAddress2());
            accountDetail.setEmail(account.getEmail());
            accountDetail.setFirstName(account.getFirstName());
            accountDetail.setLastName(account.getLastName());
            accountDetail.setCity(account.getCity());
            accountDetail.setCountry(account.getCountry());
            accountDetail.setStatus(account.getStatus());
            accountDetail.setPhone(account.getPhone());
            accountDetail.setZip(account.getZip());
            accountDetail.setState(account.getState());

            accountDetailMapper.insert(accountDetail);
            System.out.println("插入到Account");

            //插入到profile表
            profile.setUserid(account.getUsername());
            profile.setListOption(account.isListOption());
            profile.setBannerOption(account.isBannerOption());
            profile.setLanguagePreference(account.getLanguagePreference());
            profile.setFavouriteCategoryId(account.getFavouriteCategoryId());
            profileMapper.insert(profile);
            System.out.println("插入到profile");

            //插入到signon表
            signOn.setUsername(account.getUsername());
            signOn.setPassword(account.getPassword());
            signOnMapper.insert(signOn);
            System.out.println("插入到signon");

            System.out.println("插入成功啦");
            return true;
        }
        else {
            //插入失败
            System.out.println("已存在该用户，插入失败");
            return false;
        }

    }


    //更新account
    public void updateAccount(Account account) {
        //先根据传入的account找到对应的2个对象（signon对象等会儿判断了密码再找）
        accountDetail = accountDetailMapper.selectById(account.getUsername());
        profile = profileMapper.selectById(account.getUsername());

        //各自更新自己的表
        accountDetailMapper.updateById(accountDetail);
        profileMapper.updateById(profile);

        //判断密码
        if (account.getPassword() != null && account.getPassword().length() > 0) {
            signOn = signOnMapper.selectById(account.getUsername());
            signOnMapper.updateById(signOn);
        }
    }


    //添加log
    public void insertLog(Log log){
        logMapper.insert(log);
    }

    //根据username获得log
    public List<Log> getLogsByUsername(String username) {
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        List<Log> logs = logMapper.selectList(queryWrapper.eq("userid",username));
        return logs;
    }

}

